package com.momo.hotel.controller;

import com.momo.hotel.dao.pojo.TbUser;
import com.momo.hotel.dao.pojo.ext.TbTestimonialsExt;
import com.momo.hotel.service.TestimonialsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author qiang
 * @create 2019/7/5
 */
@Controller
public class TestimonialsController {
    @Resource
    TestimonialsService testimonialsService;

    /**
     * 客户评价
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("addTestimonials")
    public Object addTestimonials(HttpServletRequest request) {
        TbUser user = (TbUser) request.getSession().getAttribute("user");
        if (user != null) {
            String textarea = request.getParameter("textarea");
            if (textarea.trim().length() != 0) {
                //获取客户评价内容，用户头像，用户编号
                TbTestimonialsExt tbTestimonialsExt = new TbTestimonialsExt();
                tbTestimonialsExt.setUserid(user.getId());
                tbTestimonialsExt.setContent(textarea);
                Integer integer = testimonialsService.addTestimonials(tbTestimonialsExt);
                if (integer != 0) {
                    return "评价成功";
                } else {
                    return "评价失败";
                }
            } else {
                return "评论内容不能为空";
            }
        } else {
            return "请登陆";
        }
    }

    /**
     * 查询客户评价
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("selectTestimonials")
    public Object selectTestimonials() {
        List<TbTestimonialsExt> tbTestimonialsExts = testimonialsService.selectTestimonials();
        return tbTestimonialsExts;
    }
}
